﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using qna.Services;
using qna.Business;

namespace qna.Controls {
	public partial class QuestionForm : System.Web.UI.UserControl {

		public int UserId { get; set; }
		public IList<Subject> Subjects { get; set; }
		public Question Question { get; set; }
		public int QuestionId { get; set; }

		protected void Page_Load(object sender, EventArgs e) {
			//initial values
			if (Question != null) {
				titleInput.Text = Question.Title;
				contentInput.Text = Question.Content;
				subjectInput.SelectedValue = Question.SubjectId.ToString();
			}
			//dropdownlist
			Subjects = ((List<BusinessObject>)(new Subject().GetAll())).ConvertAll<Subject>(delegate(BusinessObject bo) {
				return (Subject)bo;
			});
			subjectInput.DataSource = Subjects;
			subjectInput.DataTextField = "name";
			subjectInput.DataValueField = "id";
			subjectInput.DataBind();
		}

		public string GetTitle() {
			return titleInput.Text;
		}
		public string GetContent() {
			return contentInput.Text;
		}
		public int GetSubject() {
			return Convert.ToInt32(subjectInput.SelectedValue);
		}

		protected void submitBtn_Click(object sender, EventArgs e) {
			var page = Parent.Page;
			int user = Convert.ToInt32(page.User.Identity.Name);
			var s = new QuestionService();
			var newQ = s.Create(GetTitle(), GetContent(), GetSubject(), user);
			Response.Redirect("~/pages/AnswerQnA.aspx?view=" + newQ.Id);
		}
	}
}